package com.demo.generate.code.service;

import com.demo.generate.code.config.CodeGeneratorConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 表信息服务类
 * 用于获取数据库表结构信息
 */
@Service
public class TableInfoService {
    
    @Autowired
    private CodeGeneratorConfig generatorConfig;
    
    /**
     * 获取数据库连接
     * 
     * @return 数据库连接
     * @throws SQLException SQL异常
     */
    private Connection getConnection() throws SQLException {
        CodeGeneratorConfig.Database dbConfig = generatorConfig.getDatabase();
        return DriverManager.getConnection(
            dbConfig.getUrl(), 
            dbConfig.getUsername(), 
            dbConfig.getPassword()
        );
    }
    
    /**
     * 获取表列表
     * 
     * @return 表名列表
     * @throws SQLException SQL异常
     */
    public List<String> getTableNames() throws SQLException {
        List<String> tableNames = new ArrayList<>();
        
        try (Connection conn = getConnection();
             ResultSet rs = conn.getMetaData().getTables(null, null, "%", new String[]{"TABLE"})) {
            
            while (rs.next()) {
                tableNames.add(rs.getString("TABLE_NAME"));
            }
        }
        
        return tableNames;
    }
    
    /**
     * 获取表的列信息
     * 
     * @param tableName 表名
     * @return 列信息列表
     * @throws SQLException SQL异常
     */
    public List<ColumnInfo> getColumns(String tableName) throws SQLException {
        List<ColumnInfo> columns = new ArrayList<>();
        
        try (Connection conn = getConnection();
             ResultSet rs = conn.getMetaData().getColumns(null, null, tableName, null)) {
            
            while (rs.next()) {
                ColumnInfo columnInfo = new ColumnInfo();
                columnInfo.setColumnName(rs.getString("COLUMN_NAME"));
                columnInfo.setDataType(rs.getString("TYPE_NAME"));
                columnInfo.setNullable("YES".equals(rs.getString("IS_NULLABLE")));
                columnInfo.setRemarks(rs.getString("REMARKS"));
                columnInfo.setColumnSize(rs.getInt("COLUMN_SIZE"));
                columns.add(columnInfo);
            }
        }
        
        return columns;
    }
    
    /**
     * 列信息类
     */
    public static class ColumnInfo {
        /**
         * 列名
         */
        private String columnName;
        
        /**
         * 数据类型
         */
        private String dataType;
        
        /**
         * 是否可为空
         */
        private boolean nullable;
        
        /**
         * 注释
         */
        private String remarks;
        
        /**
         * 列大小
         */
        private int columnSize;
        
        // Getters and Setters
        public String getColumnName() {
            return columnName;
        }
        
        public void setColumnName(String columnName) {
            this.columnName = columnName;
        }
        
        public String getDataType() {
            return dataType;
        }
        
        public void setDataType(String dataType) {
            this.dataType = dataType;
        }
        
        public boolean isNullable() {
            return nullable;
        }
        
        public void setNullable(boolean nullable) {
            this.nullable = nullable;
        }
        
        public String getRemarks() {
            return remarks;
        }
        
        public void setRemarks(String remarks) {
            this.remarks = remarks;
        }
        
        public int getColumnSize() {
            return columnSize;
        }
        
        public void setColumnSize(int columnSize) {
            this.columnSize = columnSize;
        }
    }
}